package net.csdn.business.mirror.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import net.csdn.business.common.domain.PageDto;
import net.csdn.business.common.domain.mirror.AddMirrorBlackDto;
import net.csdn.business.common.domain.mirror.MirrorListDto;
import net.csdn.business.common.domain.mirror.ProjectMirrorDataQueryDto;
import net.csdn.business.common.enums.ResultCodeEnum;
import net.csdn.business.common.exception.BusinessException;
import net.csdn.business.common.service.SuperServiceImpl;
import net.csdn.business.mirror.entity.bean.ProjectMirrorBlack;
import net.csdn.business.mirror.enums.ProjectMirrorEnum;
import net.csdn.business.mirror.mapper.ProjectMirrorBlackMapper;
import net.csdn.business.mirror.service.ProjectMirrorBlackService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class ProjectMirrorBlackServiceImpl extends SuperServiceImpl<ProjectMirrorBlackMapper, ProjectMirrorBlack> implements ProjectMirrorBlackService {

    @Override
    public void addBlack(AddMirrorBlackDto addMirrorBlackDto) {
        if (StringUtils.isEmpty(addMirrorBlackDto.getRepoPath()) || addMirrorBlackDto.getRepoPath().split("/").length != 2) {
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM, "repoPath");
        }
        LambdaQueryWrapper<ProjectMirrorBlack> queryWrapper = new QueryWrapper<ProjectMirrorBlack>().lambda();
        queryWrapper.eq(ProjectMirrorBlack::getRepoPath, addMirrorBlackDto.getRepoPath());
        ProjectMirrorBlack oBlack = baseMapper.selectOne(queryWrapper);
        if (Objects.nonNull(oBlack)) {
            return;
        }
        ProjectMirrorBlack black = ProjectMirrorBlack.builder()
                .repoPath(addMirrorBlackDto.getRepoPath())
                .operator(addMirrorBlackDto.getOperator())
                .build();
        baseMapper.insert(black);
    }

    @Override
    public void delBlack(AddMirrorBlackDto addMirrorBlackDto) {
        if (StringUtils.isEmpty(addMirrorBlackDto.getRepoPath()) || addMirrorBlackDto.getRepoPath().split("/").length != 2) {
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM, "repoPath");
        }
        LambdaQueryWrapper<ProjectMirrorBlack> queryWrapper = new QueryWrapper<ProjectMirrorBlack>().lambda();
        queryWrapper.eq(ProjectMirrorBlack::getRepoPath, addMirrorBlackDto.getRepoPath());
        ProjectMirrorBlack black = baseMapper.selectOne(queryWrapper);
        if (Objects.isNull(black)) {
            return;
        }
        baseMapper.deleteById(black);
    }

    @Override
    public List<String> getAllExistsRepoPaths(List<String> repoPaths) {
        if (CollectionUtils.isEmpty(repoPaths)) {
            return null;
        }

        return baseMapper.getAllExistsRepoPath(repoPaths);
    }

    @Override
    public ProjectMirrorBlack getByRepoPath(String repoPath) {
        if (StringUtils.isEmpty(repoPath)) {
            return null;
        }
        LambdaQueryWrapper<ProjectMirrorBlack> queryWrapper = new QueryWrapper<ProjectMirrorBlack>().lambda();
        queryWrapper.eq(ProjectMirrorBlack::getRepoPath, repoPath);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<ProjectMirrorBlack> getByRepoPaths(List<String> repoPaths) {
        if (CollectionUtils.isEmpty(repoPaths)) {
            return null;
        }
        LambdaQueryWrapper<ProjectMirrorBlack> queryWrapper = new QueryWrapper<ProjectMirrorBlack>().lambda();
        queryWrapper.in(ProjectMirrorBlack::getRepoPath, repoPaths);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public PageDto<MirrorListDto> list(ProjectMirrorDataQueryDto dto) {
        QueryWrapper<ProjectMirrorBlack> queryWrapper = new QueryWrapper<ProjectMirrorBlack>();
        LambdaQueryWrapper<ProjectMirrorBlack> lambdaQueryWrapper = queryWrapper.lambda();
        if (StringUtils.isNotEmpty(dto.getRepoPath())) {
            lambdaQueryWrapper.eq(ProjectMirrorBlack::getRepoPath, dto.getRepoPath());
        }
        if (Objects.nonNull(dto.getOrder())) {
            queryWrapper.orderBy(true, dto.getOrder().isAsc(), dto.getOrder().getName());
        } else {
            queryWrapper.orderBy(true, false, "id");
        }
        Page<ProjectMirrorBlack> page = new Page<>(dto.getPageNum(), dto.getPageSize(), dto.isSearchCount());
        baseMapper.selectPage(page, lambdaQueryWrapper);
        List<MirrorListDto> list = Lists.newArrayList();
        if (CollectionUtils.isEmpty(page.getRecords())) {
            return PageDto.build(list, ((int) page.getCurrent()), ((int) page.getSize()), page.getTotal());
        }
        for (ProjectMirrorBlack data : page.getRecords()) {
            MirrorListDto r = MirrorListDto.builder()
                    .repoPath(data.getRepoPath())
                    .source("GitHub")
                    .createdAt(data.getCreatedAt())
                    .creator(data.getOperator())
                    .status(ProjectMirrorEnum.Status._99.code)
                    .statusDesc(ProjectMirrorEnum.Status._99.trans)
                    .githubUrl("https://github.com/" + data.getRepoPath())
                    .mirrorPath("")
                    .mirrorUrl("")
                    .build();
            list.add(r);
        }
        return PageDto.build(list, ((int) page.getCurrent()), ((int) page.getSize()), page.getTotal());
    }
}